<?php


namespace app\shop\controller;

use app\admin\controller\ApiCommon;
use app\admin\model\Password;
use app\shop\model\Region as RegionModel;
use think\facade\Db;
use think\response\Json;


/**
 * Class Region
 * @package app\shop\controller
 * @author Mr.Li
 * @create 2021-05-31 10:50
 */
class Region extends ApiCommon
{
    /**
     * 用于判断权限
     * @permission 无限制
     * @allow 登录用户可访问
     * @author Mr.Li
     * @create 2021-05-30 10:48
     */
    public function initialize()
    {

        $action = [
            'permission' => [''],
            'allow' => [
                'index','compare','getjsonregion'
            ]
        ];
        event('CheckAuth', $action);
        $request = $this->request;
        $a       = strtolower($request->action());
        if (!in_array($a, $action['permission'])) {
            parent::initialize();
        }
    }

    /**
     * 获取省市区三级列表
     * @return Json
     * @author Mr.Li
     * @create 2021-05-31 10:51
     */
    public function index(): Json
    {
        $regionModel = new RegionModel();
        $data      = $regionModel->getDataListById($this->param);
        if (!$data) {
            return resultArray(['error' => $regionModel->getError()]);
        }
        return resultArray(['data' => $data]);
    }


    /**
     * 导出区域
     * @author Mr.Li
     * @create 2021-06-09 10:46
     */
    public function export()
    {
        return resultArray(['data' => []]);
        $province = Db::name('shop_region')->where('level','=',1)->select();
        $list = [];
        foreach($province as $p){
            $city = Db::name('shop_region')->where('parent_id','=',$p['id'])->select()->toArray();
            foreach($city as $c){
                $district = Db::name('shop_region')->where('parent_id','=',$c['id'])->select()->toArray();
                foreach($district as $dist){
                      $list[] = [
                          'province_name' => $p['name'],
                          'city_name'   => $c['name'],
                          'd_name'        => $dist['name'],
                      ];
                }

            }
        }
        $callName = ['province_name' => '省','city_name' =>'市','d_name' => '区县'];
        $downUrl = exportExcelData('regions',$callName,$list,'xlsx',1);
        return resultArray(['data' =>['url' => 'http://'.$_SERVER['HTTP_HOST'].$downUrl],'error' => '获取成功']);
    }


    /**
     * 区域对比补全三级
     * @author Mr.Li
     * @create 2021-08-21 16:59
     */
    public function compare()
    {
        return true;
        $insertArea = [];
        $areaCode = [];
        $city = Db::name('shop_region')->where('level','=',2)->select()->toArray();
        if($city){
            foreach($city as $v){
                $distinct = Db::name('shop_region')->where('parent_id','=',$v['id'])->select()->toArray();
                if(empty($distinct)){
                    echo $v['name']."\n";

                    $newCityData = Db::name('con_region_copy1')->where('id','=',$v['code'])->field('name,id')->find();
                    if(!empty($newCityData)){

                        $newDistinctData = Db::name('con_region_copy1')->where('pid','=',$newCityData['id'])->select()->toArray();

                        if(!empty($newDistinctData)){
                            echo $v['name']."\n";
                            foreach($newDistinctData as $distinctV)
                            {
                                $insertArea[] = [
                                    'name'      => $distinctV['name'],
                                    'level'     => 3,
                                    'code'      =>  $distinctV['id'],
                                    'parent_id' => $v['id']
                                ];
                                $areaCode[] = $distinctV['id'];
                            }

                        }
                    }
                }

            }
            if($insertArea){
                if(Db::name('shop_region')->where('code','in',$areaCode)->find()){
                    echo '不能重复插入数据';
                    return;
                }else{
                    Db::name('shop_region')->insertAll($insertArea);
                    echo '插入成功';
                }
            }else{
                echo '没有数据需要插入';
                return;
            }

        }
    }


    /**
     * 获取省市区json数据
     * @author Mr.Li
     * @create 2021-08-23 9:31
     */
    public function getJsonRegion()
    {
        return true;
        $province = Db::name('shop_region')->where('level','=',1)->select()->toArray();
        $cityList = [];

        if($province){
            foreach($province as $v){
                $area = [
                    'name'  => $v['name'],
                    'id'    => $v['id']
                ];
                $city = Db::name('shop_region')->where('parent_id','=',$v['id'])->select()->toArray();
                if(!empty($city)){
                    $cityData = [];
                    foreach($city as $cityV){
                        $distinct = Db::name('shop_region')->where('parent_id','=',$cityV['id'])->select()->toArray();
                        if(!empty($distinct)){
                            $distinctData = [];
                            foreach($distinct as $distinctV){
                                $distinctData[] = [
                                    'name'  => $distinctV['name'],
                                    'id'    => $distinctV['id']
                                ];
                            }
                        }
                        $cityData[] = [
                            'name'  => $cityV['name'],
                            'id'    => $cityV['id'],
                            'child' => $distinctData
                        ];
                    }
                    $area['child'] =$cityData;
                }

                $cityList[] =$area;
            }

        }

        echo json_encode($cityList);
        exit();
    }

}
